package com.zc.portal.infra.security.config;

import com.zc.portal.infra.security.access.RoleAccessDecisionManager;
import com.zc.portal.infra.security.filter.JwtLoginFilter;
import com.zc.portal.infra.security.filter.JwtCheckAuthFilter;
import com.zc.portal.infra.security.handler.RoleAccessDeniedHandler;
import com.zc.portal.infra.security.metadata.UrlInvocationSecurityMetadataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.config.annotation.ObjectPostProcessor;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.web.access.intercept.FilterSecurityInterceptor;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    //    @Value("${protal.security.login.url}")
    private String loginUrl = "/login";
    @Autowired
    private RoleAccessDecisionManager roleAccessDecisionManager;
    @Autowired
    private UrlInvocationSecurityMetadataSource urlInvocationSecurityMetadataSource;
    @Autowired
    private DaoAuthenticationProvider daoAuthenticationProvider;
    @Autowired
    private RoleAccessDeniedHandler roleAccessDeniedHandler;
    @Autowired
    private UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource;


    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .cors()
                .configurationSource(urlBasedCorsConfigurationSource)
                .and()
                .csrf().disable()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .authorizeRequests().anyRequest().authenticated().withObjectPostProcessor(filterSecurityInterceptorObjectPostProcessor())
                .and()
                .exceptionHandling().accessDeniedHandler(roleAccessDeniedHandler)
                .and()
                .addFilterAt(jwtLoginFilter(), UsernamePasswordAuthenticationFilter.class)
                .addFilterAt(jwtCheckAuthFilter(), BasicAuthenticationFilter.class)
                .httpBasic();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(daoAuthenticationProvider);
    }

    /**
     * @return
     * @description 设置元数据
     */
    private ObjectPostProcessor<FilterSecurityInterceptor> filterSecurityInterceptorObjectPostProcessor() {
        return new ObjectPostProcessor<FilterSecurityInterceptor>() {
            @Override
            public <O extends FilterSecurityInterceptor> O postProcess(O object) {
                object.setAccessDecisionManager(roleAccessDecisionManager);
                object.setSecurityMetadataSource(urlInvocationSecurityMetadataSource);
                return object;
            }
        };
    }

    @Bean
    public JwtLoginFilter jwtLoginFilter() throws Exception {
        return new JwtLoginFilter(loginUrl, this.authenticationManager());
    }

    @Bean
    public JwtCheckAuthFilter jwtCheckAuthFilter() throws Exception {
        return new JwtCheckAuthFilter(this.authenticationManager());
    }


}
